package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.DwaConvertCode;
import cn.com.connext.scrm.domain.master.DwaGetCoupon;
import cn.com.connext.scrm.domain.master.Prize;
import cn.com.connext.scrm.domain.master.PrizeGood;
import cn.com.connext.scrm.repository.master.DwaConvertCodeRepository;
import cn.com.connext.scrm.repository.master.DwaGetCouponRepository;
import cn.com.connext.scrm.repository.master.PrizeRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.TmpMsgRecordService;
import cn.com.connext.scrm.web.rest.vm.DwaIsReceiveVM;
import cn.com.connext.scrm.web.rest.vm.TempSubScribeVm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class DwaGetCouponService {
    @Autowired
    private DwaGetCouponRepository dwaGetCouponRepository;
    @Autowired
    private DwaConvertCodeRepository dwaConvertCodeRepository;
    @Autowired
    private PrizeGoodService prizeGoodService;
    @Autowired
    private TmpMsgRecordService tmpMsgRecordService;
    @Autowired
    private PrizeRepository prizeRepository;

    public void add(DwaIsReceiveVM vm) {
        DwaGetCoupon entity = new DwaGetCoupon();
        entity.setActivityId(vm.getActivityId());
        entity.setUnionId(vm.getUnionId());
        entity.setGoodId(vm.getGoodId());
        //领取其他优惠券
        dwaGetCouponRepository.saveAndFlush(entity);
    }

    public List<DwaGetCoupon> checkIsReceive(DwaIsReceiveVM vm) {
        List<DwaGetCoupon> list = dwaGetCouponRepository.findByActivityIdAndUnionId(vm.getActivityId(),vm.getUnionId());
        return list;
    }

    public DwaGetCoupon findByUnionId(String unionId) {
        DwaGetCoupon entity = dwaGetCouponRepository.findByUnionId(unionId);
        return entity;
    }

    public DwaConvertCode findTopCouponByUnionId(String unionId) {
        DwaConvertCode entity = dwaConvertCodeRepository.findByUnionId(unionId);
        return entity;
    }


    public  List<DwaGetCoupon> findByActivityIdAndUnionId(String activityId, String unionId) {
        List<DwaGetCoupon> list = dwaGetCouponRepository.findByActivityIdAndUnionId(activityId,unionId);
        return list;
    }

    public void getTopCoupon(String unionId) {
        DwaConvertCode code = dwaConvertCodeRepository.findByUnionId(unionId);
        if (code == null) {
            List<DwaConvertCode> list = dwaConvertCodeRepository.getRandomEntity();
            if (list != null && list.size() >0) {
                DwaConvertCode dwa = new DwaConvertCode();
                dwa.setIsUse(2);
                dwa.setUnionId(unionId);
                dwa.setId(list.get(0).getId());
                dwa.setUseDate(ZonedDateTime.now());
                dwa.setCode(list.get(0).getCode());
                //领取dwaTop优惠券
                dwaConvertCodeRepository.saveAndFlush(dwa);
            }
        }
    }

    public JsonResult addCoupon(List<PrizeGood> pglist, DwaIsReceiveVM vm) {
        JsonResult result =new JsonResult();
        Prize  prize =null;
        for (PrizeGood pg:pglist) {
               boolean flag = prizeGoodService.cutStock(pg.getId(), vm.getActivityId());
               if(flag){
                   vm.setGoodId(pg.getId());
                   add(vm);
                   prize = new Prize();
                   prize.setUnionId(vm.getUnionId());
                   //奖品ID
                   prize.setGoodId(pg.getId());
                   prize.setCreateTime(new Date());
                   prize.setUpdateTime(new Date());
                   prize.setAddPoint(false);
                   if (StringUtils.isNotEmpty(vm.getActivityId())) {
                       prize.setActivityId(vm.getActivityId());
                   }
                   prizeRepository.saveAndFlush(prize);
               }
        }
        if(vm.getIsReceive() == 1 && vm.getType()!=null && vm.getType() ==1){
            TempSubScribeVm tsm =new TempSubScribeVm();
            tsm.setOpenId(vm.getOpenId());
            tsm.setActivityId(vm.getActivityId());
            //tsm.setGoodId(goodId);
            tsm.setGoodName("优惠券");
            tsm.setPgList(pglist);
            result= tmpMsgRecordService.sendCouponTemplateMsg(tsm);
        }
            getTopCoupon(vm.getUnionId());
            result.setState(0);
            result.setMessage("领取成功");
            return result;
        }
}
